from odoo import models,_,fields
# from models import date_info,person_info
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from odoo.exceptions import UserError
import logging,datetime


logger = logging.getLogger(__name__)


class Train(models.Model):
    _name = 'train.info'
    _description = 'some details of the trains'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _rec_name = 'title'

    flag = fields.Selection(
        [
            ('yes', '是'),
            ('no', '否')
        ],
        string='是否为工坊成员', default='yes'
    )

    name = fields.Many2one(
        'res.users',
        string='培训人',
        # required=True
        default=lambda self:self.env.user,
        # readonly=True
    )

    # 非工坊成员申请培训
    other_name = fields.Char(
        string='培训人',
        # required=True
    )

    title = fields.Char('主题', required=True)

    time = fields.Datetime('培训时间', required=True)

    place = fields.Char('地点', required=True)

    desc = fields.Html('描述', required=True)

    resource = fields.Binary('附件')

    img = fields.Binary('海报')

    inform = fields.Char('实时通知')

    chief_director_comment = fields.Html('审核意见')
    comment_director_state = fields.Boolean(
        '总监评论是否显示',
        default=False
    )
    current_user_is_director = fields.Boolean(
        compute="_compute_current_user"
    )

    def _compute_current_user(self):
        self.current_user_is_group_leader = self.user_has_groups('sugon_system.asset_group_leader')
        self.current_user_is_deputy_director = self.user_has_groups('sugon_system.asset_deputy_director')
        self.current_user_is_director = self.user_has_groups('sugon_system.asset_director')
        self.current_user_is_teacher = self.user_has_groups('sugon_system.asset_teacher')

    find_user = fields.Boolean(
        compute="_computer_user"
    )

    def _computer_user(self):
        user = self.env.uid
        # sql = """select create_uid from train_info where name ={user}""" .format(user=user)
        # self.env.cr.execute(sql)
        # find_id = self.env.cr.dictfetchall()
        if user == self.name.id:
            self.find_user = True
        else:
            self.find_user = False

    find_user1 = fields.Boolean(
        compute="_computer_user1"
    )

    def _computer_user1(self):
        user = self.env.uid
        if user == self.name.id:
            self.find_user1 = True
        else:
            self.find_user1 = False

    state = fields.Selection(
        [
            (1, '发起'),
            (2, '待审'),
            (3, '已报备'),
            (4, '申请失败'),
            (5, '已结束')
        ],
        string='状态', default=1, readonly=True, copy=False, track_visibility='onchange'
    )
    is_end = fields.Boolean(
        '是否结束',
        default=False
    )
    is_back = fields.Boolean(
        '是否填写反馈',
        default=False
    )
    is_info = fields.Boolean(
        '意见是否为空',
        default=False
    )

    def button_pending_trial(self):
        self.comment_director_state = True
        retime = self.time
        now = datetime.datetime.now()
        groups = self.env['res.groups'].sudo().search([])
        group = groups.search([('name', 'like', '%技术总监%')])
        users = group.users     # 找到技术总监组的所有成员
        if int(retime.year) > int(now.year):
            for user in users:
                Train.call_owner(self, user)  # 向总监组发送邮件
            return self.write({"state": 2})
        if int(retime.year) == int(now.year):
            if int(retime.month) > int(now.month):
                for user in users:
                    Train.call_owner(self, user)  # 向总监组发送邮件
                return self.write({"state": 2})
            if int(retime.month) == int(now.month):
                if int(retime.day) > int(now.day):
                    for user in users:
                        Train.call_owner(self, user)  # 向总监组发送邮件
                    return self.write({"state": 2})
        raise UserError('申请的时间不可设定在今天之前，请重新规划您的时间')
        # sql = """SELECT email from person_info where title_rule like '技术总监'"""
        # self.env.cr.execute(sql)
        # user = self.env.cr.dictfetchall()
        # Train.call_owner(self, user)  # 向总监组发送邮件



    def call_owner(self, user):
        sender = 'zilouis@qq.com'
        passwd = 'sovlbfcttwogbhhi'
        logger.info("Here is the email addr: %s", user.email)
        receivers = user.email  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
        # 过滤非工坊成员的请求
        if not receivers:
            return self.write({"state": 2})
        else:
            mail_msg = """
                              <!doctype html>
                              <html>
                                <head>
                                  <meta name="viewport" content="width=device-width">
                                  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                                </head>
                                <body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
                                  <table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
                                    <tr>
                                      <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                      <td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">
                                        <div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">

                                          <!-- START CENTERED WHITE CONTAINER -->
                                          <span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>
                                          <table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">

                                            <!-- START MAIN CONTENT AREA -->
                                            <tr>
                                              <td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
                                                <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                                  <tr>
                                                    <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
                                                      <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">你好,总监,</p>
                                                      <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">有一封{time}在{place},主题为{title}的培训申请表,已经提交,请您尽快进行审核!</p>
                                                      <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">
                                                        <tbody>
                                                          <tr>
                                                            <td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;">
                                                              <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
                                                                <tbody>
                                                                  <tr>
                                                                    <td style="font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; border-radius: 5px; text-align: center;"> <a href="http://admin.hubusugon.cn" target="_blank" style="display: inline-block; color: #ffffff; background-color: #3498db; border: solid 1px #3498db; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none; font-size: 14px; font-weight: bold; margin: 0; padding: 12px 25px; text-transform: capitalize; border-color: #3498db;">湖北大学瑞翼工坊内部管理系统</a> </td>
                                                                  </tr>
                                                                </tbody>
                                                              </table>
                                                            </td>
                                                          </tr>
                                                        </tbody>
                                                      </table>
                                                      <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">祝您生活愉快！</p>
                                                    </td>
                                                  </tr>
                                                </table>
                                              </td>
                                            </tr>

                                          <!-- END MAIN CONTENT AREA -->
                                          </table>

                                          <!-- START FOOTER -->
                                          <div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">
                                            <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                              <tr>
                                                <td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
                                                  <span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">版权所有©湖北大学瑞翼工坊</span>
                                                </td>
                                              </tr>

                                            </table>
                                          </div>
                                          <!-- END FOOTER -->

                                        <!-- END CENTERED WHITE CONTAINER -->
                                        </div>
                                      </td>
                                      <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                    </tr>
                                  </table>
                                </body>
                              </html>
                          """.format(time=self.time, place=self.place, title=self.title)
        message = MIMEText(mail_msg, 'html', 'utf-8')
        message['From'] = Header("湖北大学瑞翼工坊", 'utf-8')
        message['To'] = Header(user.email, 'utf-8')

        subject = '湖北大学瑞翼工坊'
        message['Subject'] = Header(subject, 'utf-8')

        try:
            smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
            smtpObj.login(sender, passwd)
            smtpObj.sendmail(sender, receivers, message.as_string())
            print("邮件发送成功")
        except Exception as e:
            raise UserError(_('邮件未发送成功!原因如下:%s') % e)

    def button_edit(self):
        sender = 'zilouis@qq.com'
        passwd = 'sovlbfcttwogbhhi'
        logger.info("Here is the email addr: %s", self.name.login)
        receivers = self.name.login # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
        # 过滤非工坊成员的请求
        if not receivers:
            return self.write({"state": 1})
        else:
            mail_msg = """
                       <!doctype html>
                       <html>
                         <head>
                           <meta name="viewport" content="width=device-width">
                           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                         </head>
                         <body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
                           <table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
                             <tr>
                               <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                               <td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">
                                 <div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">

                                   <!-- START CENTERED WHITE CONTAINER -->
                                   <span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>
                                   <table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">

                                     <!-- START MAIN CONTENT AREA -->
                                     <tr>
                                       <td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
                                         <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                           <tr>
                                             <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
                                               <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">你好,{name},</p>
                                               <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">您递交的一份{time}在{place},主题为{title}的培训申请表,未通过审核,请根据{chief_director_comment}改写或者取消这次的培训!</p>
                                               <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">
                                                 <tbody>
                                                   <tr>
                                                     <td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;">
                                                       <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
                                                         <tbody>
                                                           <tr>
                                                             <td style="font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; border-radius: 5px; text-align: center;"> <a href="http://admin.hubusugon.cn" target="_blank" style="display: inline-block; color: #ffffff; background-color: #3498db; border: solid 1px #3498db; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none; font-size: 14px; font-weight: bold; margin: 0; padding: 12px 25px; text-transform: capitalize; border-color: #3498db;">湖北大学瑞翼工坊内部管理系统</a> </td>
                                                           </tr>
                                                         </tbody>
                                                       </table>
                                                     </td>
                                                   </tr>
                                                 </tbody>
                                               </table>
                                               <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">祝您生活愉快！</p>
                                             </td>
                                           </tr>
                                         </table>
                                       </td>
                                     </tr>

                                   <!-- END MAIN CONTENT AREA -->
                                   </table>

                                   <!-- START FOOTER -->
                                   <div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">
                                     <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                       <tr>
                                         <td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
                                           <span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">版权所有©湖北大学瑞翼工坊</span>
                                         </td>
                                       </tr>

                                     </table>
                                   </div>
                                   <!-- END FOOTER -->

                                 <!-- END CENTERED WHITE CONTAINER -->
                                 </div>
                               </td>
                               <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                             </tr>
                           </table>
                         </body>
                       </html>
                   """.format(name=self.name.name, time=self.time,
                              place=self.place, title=self.title, chief_director_comment=self.chief_director_comment)
        message = MIMEText(mail_msg, 'html', 'utf-8')
        message['From'] = Header("湖北大学瑞翼工坊", 'utf-8')
        message['To'] = Header(self.name.login, 'utf-8')

        subject = '湖北大学瑞翼工坊'
        message['Subject'] = Header(subject, 'utf-8')


        try:
            smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
            smtpObj.login(sender, passwd)
            smtpObj.sendmail(sender, receivers, message.as_string())
            print("邮件发送成功")
        except Exception as e:
            raise UserError(_('邮件未发送成功!原因如下:%s') % e)
        return self.write({"state": 1})

    def button_reported(self):
        sender = 'zilouis@qq.com'
        passwd = 'sovlbfcttwogbhhi'
        logger.info("Here is the email addr: %s", self.name.login)
        receivers = self.name.login  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
        # 过滤非工坊成员的请求
        if not receivers:
            # 添加到日历表里
            info = {
                'name': self.other_name,
                'schedule_date': self.time,
            }
            record = self.env['date.info'].create(info)
            return self.write({"state": 3})
        else:
            mail_msg = """
                          <!doctype html>
                          <html>
                            <head>
                              <meta name="viewport" content="width=device-width">
                              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                            </head>
                            <body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
                              <table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
                                <tr>
                                  <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                  <td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">
                                    <div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">

                                      <!-- START CENTERED WHITE CONTAINER -->
                                      <span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>
                                      <table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">

                                        <!-- START MAIN CONTENT AREA -->
                                        <tr>
                                          <td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
                                            <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                              <tr>
                                                <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
                                                  <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">{name},</p>
                                                  <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">您递交的一份{time}在{place},主题为{title}的培训申请表,已经通过审核!</p>
                                                  <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">
                                                    <tbody>
                                                      <tr>
                                                        <td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;">
                                                          <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
                                                            <tbody>
                                                              <tr>
                                                                <td style="font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; border-radius: 5px; text-align: center;"> <a href="http://admin.hubusugon.cn" target="_blank" style="display: inline-block; color: #ffffff; background-color: #3498db; border: solid 1px #3498db; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none; font-size: 14px; font-weight: bold; margin: 0; padding: 12px 25px; text-transform: capitalize; border-color: #3498db;">湖北大学瑞翼工坊内部管理系统</a> </td>
                                                              </tr>
                                                            </tbody>
                                                          </table>
                                                        </td>
                                                      </tr>
                                                    </tbody>
                                                  </table>
                                                  <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">祝您生活愉快！</p>
                                                </td>
                                              </tr>
                                            </table>
                                          </td>
                                        </tr>

                                      <!-- END MAIN CONTENT AREA -->
                                      </table>

                                      <!-- START FOOTER -->
                                      <div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">
                                        <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                          <tr>
                                            <td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
                                              <span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">版权所有©湖北大学瑞翼工坊</span>
                                            </td>
                                          </tr>

                                        </table>
                                      </div>
                                      <!-- END FOOTER -->

                                    <!-- END CENTERED WHITE CONTAINER -->
                                    </div>
                                  </td>
                                  <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                </tr>
                              </table>
                            </body>
                          </html>
                      """.format(name=self.name.name, time=self.time, place=self.place, title=self.title)
        message = MIMEText(mail_msg, 'html', 'utf-8')
        message['From'] = Header("湖北大学瑞翼工坊", 'utf-8')
        message['To'] = Header(self.name.login, 'utf-8')

        subject = '湖北大学瑞翼工坊'
        message['Subject'] = Header(subject, 'utf-8')

        try:
            smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
            smtpObj.login(sender, passwd)
            smtpObj.sendmail(sender, receivers, message.as_string())
            print("邮件发送成功")
        except Exception as e:
            raise UserError(_('邮件未发送成为!原因如下:%s') % e)
        # 将培训信息添加到日历表
        info = {
            'name': self.name.name,
            'schedule_date': self.time,
        }
        record = self.env['date.info'].create(info)
        return self.write({"state": 3})

    def button_fail(self):
        sender = 'zilouis@qq.com'
        passwd = 'sovlbfcttwogbhhi'
        logger.info("Here is the email addr: %s", self.name.login)
        receivers = self.name.login  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
        # 过滤非工坊成员的请求
        if not receivers:
            return self.write({"state": 4})
        else:
            mail_msg = """
                          <!doctype html>
                          <html>
                            <head>
                              <meta name="viewport" content="width=device-width">
                              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                            </head>
                            <body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
                              <table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
                                <tr>
                                  <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                  <td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">
                                    <div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">

                                      <!-- START CENTERED WHITE CONTAINER -->
                                      <span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>
                                      <table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">

                                        <!-- START MAIN CONTENT AREA -->
                                        <tr>
                                          <td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
                                            <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                              <tr>
                                                <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
                                                  <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">{name},</p>
                                                  <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">您递交的一份{time}在{place},主题为{title}的培训申请表,已经被否决,原因是:{chief_director_comment}</p>
                                                  <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">
                                                    <tbody>
                                                      <tr>
                                                        <td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;">
                                                          <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
                                                            <tbody>
                                                              <tr>
                                                                <td style="font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; border-radius: 5px; text-align: center;"> <a href="http://admin.hubusugon.cn" target="_blank" style="display: inline-block; color: #ffffff; background-color: #3498db; border: solid 1px #3498db; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none; font-size: 14px; font-weight: bold; margin: 0; padding: 12px 25px; text-transform: capitalize; border-color: #3498db;">湖北大学瑞翼工坊内部管理系统</a> </td>
                                                              </tr>
                                                            </tbody>
                                                          </table>
                                                        </td>
                                                      </tr>
                                                    </tbody>
                                                  </table>
                                                  <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">祝您生活愉快！</p>
                                                </td>
                                              </tr>
                                            </table>
                                          </td>
                                        </tr>

                                      <!-- END MAIN CONTENT AREA -->
                                      </table>

                                      <!-- START FOOTER -->
                                      <div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">
                                        <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                          <tr>
                                            <td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
                                              <span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">版权所有©湖北大学瑞翼工坊</span>
                                            </td>
                                          </tr>

                                        </table>
                                      </div>
                                      <!-- END FOOTER -->

                                    <!-- END CENTERED WHITE CONTAINER -->
                                    </div>
                                  </td>
                                  <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                </tr>
                              </table>
                            </body>
                          </html>
                      """.format(name=self.name.name, time=self.time, chief_director_comment=self.chief_director_comment,
                                 place=self.place, title=self.title)
        message = MIMEText(mail_msg, 'html', 'utf-8')
        message['From'] = Header("湖北大学瑞翼工坊", 'utf-8')
        message['To'] = Header(self.name.login, 'utf-8')

        subject = '湖北大学瑞翼工坊'
        message['Subject'] = Header(subject, 'utf-8')

        try:
            smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
            smtpObj.login(sender, passwd)
            smtpObj.sendmail(sender, receivers, message.as_string())
            print("邮件发送成功")
        except Exception as e:
            raise UserError(_('邮件未发送成为!原因如下:%s') % e)
        return self.write({"state": 4})

    def return_end(self):
        self.ensure_one()
        if self.is_end:
            raise UserError(_('%s已结束!') % self.title)
        # 有则报错
        else:
            return self.write({"state": 5})

    def train_back(self):
        self.ensure_one()
        if self.is_back:
            raise UserError(_('您已反馈过%s，请勿重复反馈!') % self.title)
        # 有则报错
        else:

            try:
                self.write({"is_back":True})

                return {
                    'type': 'ir.actions.act_window',
                    'view_type': 'form',
                    'view_mode': 'form,tree',
                    'res_model': 'train.feedback',
                    'target': 'main',
                    'context': {
                        'default_name': self.id
                    }
                }

            except Exception as e:
                raise UserError(_('错误：%s') % e)
